package walletadminlogic

import (
	"context"
	"github.com/shopspring/decimal"
	"go.uber.org/zap"
	"navi-mumbai/app/rpc/model/pg/wallet"
	"navi-mumbai/common/key/dbkey"
	"navi-mumbai/common/key/walletkey"
	"navi-mumbai/common/utils"
	"navi-mumbai/common/xerr"
	"time"

	"navi-mumbai/app/rpc/wallet/internal/svc"
	"navi-mumbai/app/rpc/wallet/pb"

	"github.com/zeromicro/go-zero/core/logx"
)

type QueryUserDepositFlowListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewQueryUserDepositFlowListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *QueryUserDepositFlowListLogic {
	return &QueryUserDepositFlowListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 管理后台查询用户充值流水列表
func (l *QueryUserDepositFlowListLogic) QueryUserDepositFlowList(in *pb.QueryUserDepositFlowListReq) (*pb.QueryUserDepositFlowListResp, error) {
	defer utils.RecoverPanic()

	var start, end *time.Time
	if in.StartTime != "" {
		loc, _ := time.LoadLocation(dbkey.DateTimeZone)
		s, err := time.ParseInLocation(dbkey.DateTimeCNFormat, in.StartTime, loc)
		if err != nil {
			l.Logger.Errorf("err == ", zap.Error(err))
			return nil, xerr.NewGrpcErrCodeMsg(xerr.RequestParamError, err.Error())
		}
		start = &s
	}
	if in.EndTime != "" {
		loc, _ := time.LoadLocation(dbkey.DateTimeZone)
		e, err := time.ParseInLocation(dbkey.DateTimeCNFormat, in.EndTime, loc)
		if err != nil {
			l.Logger.Errorf("err == ", zap.Error(err))
			return nil, xerr.NewGrpcErrCodeMsg(xerr.RequestParamError, err.Error())
		}
		end = &e
	}
	var list []*wallet.UserDepositFlow
	var err error
	list, err = l.svcCtx.UserDepositFlowModel.Find(l.ctx, in.Uid, in.State, in.MoneyType, in.CurrencyType, in.PayChannelId, start, end, in.PageNo, in.PageSize)
	if err != nil {
		l.Logger.Errorf("err == ", zap.Error(err))
		return nil, xerr.NewGrpcErrCodeMsg(xerr.DbError, err.Error())
	}
	var resp pb.QueryUserDepositFlowListResp
	resp.List = make([]*pb.UserDepositFlow, 0)
	if len(list) <= 0 {
		return &resp, nil
	}

	//
	if in.PageNo <= 1 { // 第一页 返回总页数
		resp.Total, err = l.svcCtx.UserDepositFlowModel.Count(l.ctx, in.Uid, in.State, in.MoneyType, in.CurrencyType, in.PayChannelId, start, end)
		if err != nil {
			l.Logger.Errorf("err == ", zap.Error(err))
			return nil, xerr.NewGrpcErrCodeMsg(xerr.DbError, err.Error())
		}
	}

	for i := 0; i < len(list); i++ {
		var v pb.UserDepositFlow
		v.FlowNo = list[i].FlowNo
		v.Uid = list[i].Uid
		v.State = list[i].State
		v.MoneyType = list[i].MoneyType
		v.CurrencyType = list[i].CurrencyType
		v.PayChannelId = list[i].PayChannelId
		v.MoneyAmount = decimal.NewFromInt(list[i].MoneyAmount).Div(decimal.NewFromFloat(walletkey.MoneyScale)).StringFixedBank(2)
		v.CurrencyAmount = decimal.NewFromInt(list[i].CurrencyAmount).Div(decimal.NewFromFloat(walletkey.MoneyScale)).StringFixedBank(2)
		v.CreateTime = list[i].CreateTime.Format(dbkey.DateTimeCNFormat)
		v.UpdateTime = list[i].UpdateTime.Format(dbkey.DateTimeCNFormat)
		v.ExFlow = list[i].ExternalFlowNo
		v.PayAccount = list[i].PayAccount

		resp.List = append(resp.List, &v)
	}

	return &resp, nil
}
